﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using W.Purchase.API.Application.Command.FinanceTypeCommand;

namespace W.Purchase.API.Controllers
{
    /// <summary>
    /// 财务类型控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinanceTypeController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<FinanceTypeController> _logger;    
        public FinanceTypeController(IMediator mediator, ILogger<FinanceTypeController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 财务类型添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetFinanceTypeAdd(FinanceTypeCreateCommand command)
        {
            ApiResult<int> result= new ApiResult<int>();    
            try
            {
                _logger.LogInformation("调用财务类型添加");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.CreateName = name;
                command.user_id = Convert.ToInt32(uid);
                command.office_id = sid;
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类型添加出错"+ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 财务类型删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetFinanceTypeDelete(FinanceTypeDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用财务类型删除");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类型删除出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 财务类型批量删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetFinanceTypeRangeDelete(FinanceTypeRangeDeleteCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用财务类型批量删除");
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类型批量删除出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 财务类型修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<int>> GetFinanceTypeUpdate(FinanceTypeUpdateCommand command)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                _logger.LogInformation("调用财务类型修改");
                var uid = HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                var name = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Name)?.Value;//获取名称
                command.UpdateName = name;
                command.user_id = Convert.ToInt32(uid);
                command.office_id = sid;
                result.Code = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类型修改出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
    }
}
